// pages/my/changePhone/index.js
const app = getApp();
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    name: 0,
    dept: 0,
    grade: 0,
    classes: 0,
    phone: 0,
    gradeShow: false,
    curType2: {},
    options2: []
  },
  //获取用户输入的学号
  inputId: function(e) {
    this.setData({
      id: e.detail.value
    })
  },
  //获取用户输入的姓名
  inputName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //获取用户输入的系部
  changeDept: function(e) {
    var dept = this.data.options[e.detail.id].role_name;
    this.setData({
      dept: dept
    })
  },
  //获取用户输入的年级
  changeGrade: function(e) {
    var grade = this.data.options2[e.detail.id].role_name;
    this.setData({
      grade: grade
    })
  },
  //获取用户输入的班级
  inputClass: function(e) {
    this.setData({
      classes: e.detail.value
    })
  },
  //获取用户输入的手机号
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取当前时间 得到当前最近四年
  time: function() {
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var Y = date.getFullYear();
    var M = date.getMonth();
    var year = [];
    if (M < 6) {
      Y = Y - 1
    }
    for (let i = 0; i < 4; i++) {
      year.push({
        'role_id': i,
        'role_name': Y - i - 2000
      })
    }
    that.setData({
      options2: year,
    })
    console.log(year)
    console.log(this.data.options2)
    that.setData({
      gradeShow: true
    })
  },
  submit() {
    let id = this.data.id
    let name = this.data.name
    let dept = this.data.dept
    let grade = this.data.grade
    let classes = this.data.classes
    let phone = this.data.phone
    console.log(id)
    console.log(name)
    console.log(dept)
    console.log(grade)
    console.log(classes)
    console.log(phone)
    var phoneRepExp = /^1[0-9]{10}$/;
    if (!phoneRepExp.test(phone) || phone == "") { //有问题
      wx.showToast({
        title: '手机号码有误！',
        icon: 'none',
        duration: 1000
      });
      return false;
    }
    wx.request({
      url: app.globalData.BASE_URL + '/student/lifeMinisterUpdate2',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8;charset=utf-8' // 默认值
      },
      data: {
        token: app.globalData.token,
        userType: app.globalData.userType,
        id: id,
        name: name,
        dept: dept,
        grade: grade,
        classes: classes,
        phone: phone
      },
      success(res) {
        console.log(res)
        if (res.data == 1) {
          wx.showToast({
              title: '修改成功！',
              icon: 'success',
              duration: 1500
            }),
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
        } else if (res.data == 2) {
          wx.showToast({
            title: '修改失败！',
            icon: 'success',
            duration: 1500
          })
        } else {
          wx.navigateTo({
            url: "/pages/login/index?flag=invalid",
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.time()
    let model = JSON.parse(options.model);
    // console.log(options.model)
    if (model == undefined) return;
    let idx = 0,
      idx2 = 0;
    console.log(model)
    for (var i = 0; i < this.data.options2.length; i++) {
      if (this.data.options2[i].role_name == model.name.grade) {
        idx2 = i;
        break;
      }
    }
    this.setData({
      id: model.name.id,
      name: model.name.name,
      dept: app.globalData.user.dept,
      grade: model.name.grade,
      classes: model.name.classes,
      phone: model.name.phone,
      curType2: {
        id: this.data.options2[idx2].role_id,
        name: this.data.options2[idx2].role_name,
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})